//
//  ScopeTopUpViewController.m
//  JxCarios
//
//  Created by phev on 15/2/28.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ScopeTopUpViewController.h"
#import "CLUIBaseNavigationController.h"

static NSString *checkCell = @"scopeCheck";
static NSString *shuruCell = @"scopeTopUp";

@interface ScopeTopUpViewController () {
    ScopeTopUpTableViewCell *cell;
    NSDictionary *oilInfo;
    ScopeCheckTableViewCell *seleteCell;
}

@end

@implementation ScopeTopUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self netRequest];
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    UIButton *btn = (UIButton *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{

}


- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    
    [_carAround getScopeConvertRule:^(id result) {
        _tableDataSource = [result customObjectForkey:@"exchange_rule_array"];
        _availableScope = [result customObjectForkey:@"mvb"];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
}

- (void)initDataSource {
    if (_tableDataSource.count <= 0 || !_tableDataSource) {
        return;
    }
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
}

- (void)initAbout {
    self.title = @"积分充值";
    if (!_cellArr) {
        _cellArr = [NSMutableArray new];
    }
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:@selector(backTo) andCustom:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"ScopeCheckTableViewCell" bundle:nil] forCellReuseIdentifier:checkCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ScopeTopUpTableViewCell" bundle:nil] forCellReuseIdentifier:shuruCell];
    
}

- (void)backTo {
    self.tabBarController.tabBar.hidden = NO;
    UIButton *btn = (UIButton *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView Delegate & dataSource 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return _tableDataSource.count == 0 ? 0 : _tableDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:shuruCell forIndexPath:indexPath];
        cell.target = self;
        cell.action = @selector(inputCellAction:);
        cell.vb = _availableScope;
        return cell;
    }else {
        ScopeCheckTableViewCell *check = [tableView dequeueReusableCellWithIdentifier:checkCell forIndexPath:indexPath];
        check.info = [_tableDataSource objectAtIndex:indexPath.row];
        check.target = self;
        check.action = @selector(checkCellAction:);
        if (_cellArr.count != _tableDataSource.count) {
             [_cellArr addObject:check];
        }
        return check;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    UITableViewCell *_cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([_cell isKindOfClass:[ScopeCheckTableViewCell class]]) {
        ScopeCheckTableViewCell *check = (ScopeCheckTableViewCell *)_cell;
        [check  scopeCheckAction:check.checkBtn];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 65;
    }else{
        return 44;
    }
}

- (void)inputCellAction:(NSString *)text {
    if (text.length <= 0) {
    }else{
        _payView.hidden = NO;
        _seleteVb = text;
        _payPrice.text = [RMB stringByAppendingString:[NSString stringWithFormat:@"%d",[_seleteVb integerValue]]];
        _payScope.text = [[NSString stringWithFormat:@"%d",[text integerValue] * 100] stringByAppendingString:@"分"];
        seleteCell.checkBtn.selected = NO;
    }
}

- (void)checkCellAction:(NSDictionary *)info {
    _payView.hidden = NO;
    seleteCell = (ScopeCheckTableViewCell *)[info customObjectForkey:@"cell"];
    _seleteVb = [seleteCell.info customObjectForkey:@"VB"];
    _payPrice.text = [RMB stringByAppendingString:[NSString stringWithFormat:@"%d",[_seleteVb integerValue] / 100 ]];
    _payScope.text = [_seleteVb stringByAppendingString:@"分"];
    cell.textScope.text = @"";
    if (_tableDataSource.count > 1) {
        for (ScopeCheckTableViewCell *checkCell in _cellArr) {
            if (checkCell != seleteCell) {
                checkCell.checkBtn.selected = NO;
            }
        }
    }
}

- (IBAction)payAction:(id)sender {
    if (_seleteVb && _payPrice.text.length > 0 && _payScope.text.length >0) {
        ScopePayViewController *payView = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"scopePay"];
        CLUIBaseNavigationController *base = [[CLUIBaseNavigationController alloc] initWithRootViewController:payView];
        payView.payPrice = _payPrice.text;
        payView.payScope = _payScope.text;
        [self.navigationController pushViewController:payView animated:YES];
    }
}
@end
